package exercise01;

import javafx.scene.effect.Light;

import java.util.Scanner;


public class ShoppingMSM {
    Scanner in = new Scanner(System.in);
    String[] goodsNo = new String[100];//商品编号
    String[] goodsName = new String[100];//商品名称
    double[] goodsPrice = new double[100];//商品价格
    int index = 0;//商品插入的位置，商品的数量-1


    public void start() {
        while (true) {


            //1显示---菜单
            System.out.println("---------系统菜单---------");
            System.out.println("-----1-进入系统-----------");
            System.out.println("-----2-备份数据-----------");
            System.out.println("-----3-退出系统-----------");
            System.out.println("--输入数字123，选择功能-----");
            //2-输入菜单选项
            int number = in.nextInt();
            //3-根据输入选项分支
            switch (number) {
                case 1:
                    login();
                    break;
                case 2:
                    break;
                case 3:
                    System.exit(0);
                    break;
                default:
                    System.out.println("输入数字1-3");
            }
        }
    }

    private void login() {
        //循环三次的for循环
        for (int count = 1; count <= 3; count++) {
            String name;
            String password;
            //输入用户名和密码
            System.out.println("输入用户名：");
            name = in.next();//接收一个字符串
            System.out.println("输入密码：");
            password = in.next();
            //验证
            if (name.equals("admin") && password.equals("1234")) {
                //满足、条件，验证通过
                System.out.println("验证通过,进入系统");
                showMainMenu();//进入系统，显示主菜单
                break;//跳出循环
            } else {
                //不匹配
                System.out.println("用户名和密码不匹配，请重新输入");
            }
        }
    }

    private void showMainMenu() {

        while (true) {
            int returnflag=0;


            System.out.println("---------欢迎----------");
            System.out.println("  1-商品管理");
            System.out.println("  2-会员管理");
            System.out.println("  3-购物结算");
            System.out.println("  4-返回");
            System.out.println("-----------------------");
            System.out.println("输入数字1-4，选择菜单功能");
            //自己编写多分支的代码
            //根据输入数字,跳转到相应的功能
            String no = in.next();
            switch (no) {
                case "1":
                    System.out.println("  1-商品管理");
                    goodManage();
                    break;
                case "2":
                    System.out.println("  2-会员管理");
                    break;
                case "3":
                    System.out.println("  3-购物结算");
                    checkout();
                    break;
                case "4":
                    System.out.println("  4-返回");
                    returnflag=1;
                    break;//跳出分支
                default:
                    System.out.println("输入数字1-4");
            }
            if (returnflag==1){
                break;//跳出子菜单
            }
        }
    }

    private void checkout() {
        //结账
        Scanner in = new Scanner(System.in);
        String[] shoppingCart = new String[100];//购物车，放结账的商品编号
        int shoppingCount = 0;   //结账商品的数量
        double sum = 0;//商品总金额
        float pay;//付款金额
        System.out.println("是否为会员(是:1,否:0)");
        int vipflag = in.nextInt();
        if (vipflag == 1) {
            vipCheckout();//会员结账
        } else {
            //普通顾客结账
            //结账是输入商品编号，找到商品价格，进行累加，如果输入商品编号为---，结账结束，显示小票
            while (true) {
                System.out.println("输入商品编号");
                String no = in.next();
                if (no.equals("---")) {
                    break;//输入---退出循环，显示小票
                }
                int infoindex = findGoodByNo(no);
                if (infoindex != -1) {
                    //找到商品，显示商品信息，并将其放入购物车
                    System.out.println("商品名称为" + goodsName[infoindex] + "商品单价为" + goodsPrice[infoindex]);
                    shoppingCart[shoppingCount++] = no;
                    System.out.println("商品数量=" + shoppingCount);

                } else {
                    System.out.println("商品号不存在，重新输入");

                }
            }
            //退出循环，显示小票
            //遍历购物车数组，查找商品信息并显示
            System.out.println("商品编号\t\t\t商品名称\t\t\t商品价格");
            for (int i = 0; i < shoppingCount; i++) {

                System.out.print(shoppingCart[i]+ "\t\t\t");
                int index = findGoodByNo(shoppingCart[i] );
                System.out.print(goodsName[index]+ "\t\t\t" );
                System.out.print(goodsPrice[index]);
                System.out.println("");
                sum = sum + goodsPrice[index];
            }
            System.out.println("总金额：" + sum);

        }

    }

    private int findGoodByNo(String no) {
        //根据商品no。找到商品在商品数组的位置index，未找到返回-1
        for (int i = 0; i < index; i++) {
            if (goodsNo[i].equals(no)) {
                return i;
            }
        }
        return -1;

    }

    private void vipCheckout() {
    }

    private void goodManage() {
        // TODO Auto-generated method stub
        int flag = 0;
        while (true) {
            System.out.println("-------欢迎--------");
            System.out.println("1-添加商品 ");
            System.out.println("2-修改商品");
            System.out.println("3-查询");
            System.out.println("4-返回");
            System.out.println("------------------");
            System.out.print("请输入数字(1-4):");
            int inputNumber;
            Scanner scanner = new Scanner(System.in);
            inputNumber = scanner.nextInt();
            switch (inputNumber) {
                case 1:
                    //添加商品
                    addGoods();//ctrl+鼠标点击，快速跳转到方法
                    break;
                case 2:
                    //修改商品
                    updateGoods();
                    break;
                case 3:
                    //查询			
                    showAll();
                    break;
                case 4:
                    //返回
                    //要返回上一级,先跳出switch
                    flag = 1;//将返回标志设置为1
                    break;
            }//switch结束
            //跳出switch,判断返回标志
            //标志=1,跳出循环,
            if (flag == 1) {
                break;//跳出循环
            }
        }//循环结束
    }

    private void showAll() {

        System.out.println();
        System.out.println("=======商品库存清单=======");
        System.out.println("商品编号     商品名称      商品单价");
        for (int i = 0; i < goodsNo.length; i++) {
            System.out.println(goodsNo[i] + "  " + goodsName[i] + "  " + goodsPrice[i]);
            if (i == index - 1) {
                break;//已经显示到数组的最后一个元素，
            }
        }


    }

    private void updateGoods() {
        String name;
        System.out.println("请输入商品的编号");
        Scanner in = new Scanner(System.in);
        String sr = in.next();
        for (int i = 0; i < goodsNo.length; i++) {
            if (sr.equals(goodsNo[i])) {
                System.out.println("输入新的商品编号");
                String newNo = in.next();
                goodsNo[i] = newNo;
                System.out.println("输入新的商品名字");
                String newName = in.next();
                goodsName[i] = newName;
                System.out.println("输入新的商品价格");
                double newPrice = in.nextDouble();
                goodsPrice[i] = newPrice;
                System.out.println("商品修改成功");
                return;//修改完成后，返回
            }
        }
        //没有返回，说明，编号未找到
        System.out.println("编号未找到");
    }


    private void addGoods() {
        // TODO Auto-generated method stub
        //编号，名，单价
        String name;//商品名
        Scanner in = new Scanner(System.in);
        System.out.println("请输入商品名称:");
        name = in.next();
        goodsName[index] = name;//将输入的商品名称放在数组的index位置
        System.out.println("请输入商品编号:");
        String no = in.next();//商品编号
        goodsNo[index] = no; //将输入的商品编号放在数组的index位置
        System.out.println("请输入商品价格:");
        double price = in.nextDouble();//商品价格
        goodsPrice[index] = price; //将输入的商品价格放在数组的index位置
        index++;//插入位置后移
        System.out.println("商品添加成功");
        System.out.println("商品数量=" + index);

    }


    public static void main(String[] args) {
        new ShoppingMSM().start();
    }
}
